//
//  SearchHitsViewController.m
//  ClinicalCollections
//
//  Created by janakiraman gopinath on 3/10/13.
//  Copyright (c) 2013 GOPI. All rights reserved.
//

#import "RecentViewsController.h"

@interface RecentViewsController ()

@property (strong, nonatomic) IBOutlet UITableView *tableView;

@end

@implementation RecentViewsController

@synthesize recents  = _recents;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.recents = [[CommonAPI class] getRecents];
    [self.tableView reloadData];
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.recents.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"recents";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    NSString *nctId = [[[[self.recents allObjects]
                        objectAtIndex:indexPath.row]
                        componentsSeparatedByString:@","]
                        objectAtIndex:0];
    NSString *searchString = [[[[self.recents allObjects]
                         objectAtIndex:indexPath.row]
                        componentsSeparatedByString:@","]
                       objectAtIndex:1];
    UIManagedDocument * database = [[DatabaseHelper class] database];
    
    ClinicalTrialGov *clinicalTrialGov = [[ClinicalTrialGov class] clinicalTrialGov:nctId
                                                                             search:searchString
                                                                            context:database.managedObjectContext];
    
    
    NSArray *conditionArray = [clinicalTrialGov.condition allObjects];
    NSString *titleString, *subTitleString;

    for(int i = 0; i < conditionArray.count; i++)
        if(!i) titleString = [NSString stringWithFormat:@"%@" , ((Condition *)conditionArray[i]).condition];
        else
            titleString = [NSString stringWithFormat:@"%@,%@", titleString, ((Condition *)conditionArray[i]).condition];
    
    subTitleString= [NSString stringWithFormat:@"%@,%@" , clinicalTrialGov.nctID, clinicalTrialGov.briefTitle];
    
    
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@,%@",titleString, searchString];
    cell.detailTextLabel.text = subTitleString;
    
    return cell;
}


#pragma mark - Table view delegate


- (void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
 forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

        NSString *nctID = [[cell.detailTextLabel.text componentsSeparatedByString:@","] objectAtIndex:0];
        
         [[CommonAPI class] removeRecentsNCTID:nctID];
         [self.tableView reloadData];
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
   
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    NSString *nctID = [[cell.detailTextLabel.text componentsSeparatedByString:@","] objectAtIndex:0];
    NSString *searchString = [[cell.textLabel.text componentsSeparatedByString:@","] lastObject];
    [[CommonAPI class] sendNotification:RECENT_EVENTS
                            usingObject:[NSString stringWithFormat:@"%@,%@",nctID, searchString]];
}

@end
